package com.ruoyi.bizsys.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bizsys.domain.AppUser;
import com.ruoyi.bizsys.domain.AppUserReason;
import com.ruoyi.bizsys.dto.UserInfo;
import com.ruoyi.bizsys.enums.BankStatusEnum;
import com.ruoyi.bizsys.enums.CustCardStatusEnum;
import com.ruoyi.bizsys.enums.RealNameStatusEnum;
import com.ruoyi.bizsys.enums.UserStatusEnum;
import com.ruoyi.bizsys.mapper.*;
import com.ruoyi.bizsys.service.IAppUserReasonService;
import com.ruoyi.bizsys.service.IAppUserService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;


/**
 * 用户Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-30
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService {

    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private RealNameAuthMapper realNameAuthMapper;

    @Autowired
    private IAppUserReasonService iAppUserReasonService;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private BankAccountMapper bankAccountMapper;

    @Autowired
    private CustBankCardMapper custBankCardMapper;

    /**
     * 查询用户
     *
     * @param userId 用户ID
     * @return 用户
     */
    @Override
    public AppUser selectAppUserById(Long userId) {
        return this.getById(userId);
    }

    /**
     * 查询用户列表
     *
     * @param appUser 用户
     * @return 用户
     */
    @Override
    public List<AppUser> selectAppUserList(AppUser appUser) {
        return this.list(new QueryWrapper<>(appUser).last("order by user_id desc"));
    }

    /**
     * 新增用户
     *
     * @param appUser 用户
     * @return 结果
     */
    @Override
    public int insertAppUser(AppUser appUser) {
        appUser.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(appUser);
    }

    /**
     * 修改用户
     *
     * @param appUser 用户
     * @return 结果
     */
    @Override
    public int updateAppUser(AppUser appUser) {
        appUser.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(appUser);
    }

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的用户ID
     * @return 结果
     */
    @Override
    public int deleteAppUserByIds(Long[] userIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(userIds));
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteAppUserById(Long userId) {
        return baseMapper.deleteById(userId);
    }

    @Override
    public List<String> selectUnRegisterUserList() {
        return appUserMapper.selectUnRegisterUserList();
    }

    @Override
    public AppUser getOneByUserId(String userId) {
        return getOne(new QueryWrapper<AppUser>().eq("user_uuid", userId));
    }

    @Override
    public UserInfo getUserInfoByUserId(String userId) {
        return appUserMapper.getUserInfoByUserId(userId);
    }

    @Override
    public UserInfo getUserInfoByCustNo(String custNo) {
        return appUserMapper.getUserInfoByCustNo(custNo);
    }

    @Override
    public boolean accountIsExist(String phone) {
        QueryWrapper<AppUser> queryWrapper = new QueryWrapper<AppUser>().eq("phone", phone).ne("state", UserStatusEnum.CANCELLATION.getCode());
        return count(queryWrapper) > 0;
    }

    @Override
    public JSONObject getInviteInfo(String userId) {
        AppUser one = getOne(new QueryWrapper<AppUser>().eq("user_uuid", userId).select("invite_link", "invite_code"));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("inviteLink", one.getInviteLink());
        jsonObject.put("inviteCode", one.getInviteCode());
        return jsonObject;
    }

    @Override
    public boolean updateInviteLink(String userId, String inviteLink) {
        UpdateWrapper<AppUser> userUpdateWrapper = new UpdateWrapper<AppUser>().set("invite_link", inviteLink).eq("user_uuid", userId);
        return update(userUpdateWrapper);
    }

    @Override
    public int inviteRelationCount(String inviteCode) {
        return count(new QueryWrapper<AppUser>().eq("recommend_code", inviteCode));
    }

    @Override
    public List<AppUser> inviteRelation(String inviteCode) {
        return list(new QueryWrapper<AppUser>().eq("recommend_code", inviteCode).orderByDesc("create_time"));
    }

    //冻结/解冻
    @Override
    public boolean freezeInviteState(String userId,String inviteCodeStatus) {
        UpdateWrapper<AppUser> updateWrapper = new UpdateWrapper<AppUser>().set("invite_code_status", inviteCodeStatus).eq("user_uuid", userId);
        return update(updateWrapper);
    }

    @Override
    public List<Long> get8DigitInviteCodeUser() {
        return baseMapper.get8DigitInviteCodeUser();
    }

    @Override
    public boolean getInviteCOdeCount(String inciteCode) {
        return count(new QueryWrapper<AppUser>().eq("invite_code", inciteCode)) == 0;
    }

    @Override
    public List<String> getInviteList(List<String> inviteList) {
        return baseMapper.getInviteList(inviteList);
    }

    @Override
    public String selectAppUserByPhone(String userId) {
        return baseMapper.selectAppUserByPhone(userId);
    }

    @Override
    public AppUser getSuperior(String recommendCode) {
        return getOne(new QueryWrapper<AppUser>().eq("invite_code",recommendCode));
    }

}